﻿<link href="~/miniui_net/demo/demo.css" rel="stylesheet" />
<style>
        body, html {
            margin: 0;
            padding: 0;
            width: 100%;
            height: 100%;
        }
    .CROD {
        height: 24%;
    }
    .query {
        height: 76%;
        padding:5px;
    }
    .query, .CROD {
        border: 1px solid rgb(215,235,249);
    }
    </style>
<div id="layout1" class="mini-layout" style="width: 100%; height: 100%;">
    <div title="north" region="north" height="110" showHeader="false">
        <div class="CROD">
            <a class="mini-button" img="/miniui_net/scripts/miniui/themes/icons/add.gif" onclick="CreateUseronClick">增加</a>
            <a class="mini-button" img="/miniui_net/scripts/miniui/themes/icons/edit.gif" onclick="UpdateUseronClick">修改</a>
            <a class="mini-button" img="/miniui_net/scripts/miniui/themes/icons/cancel.gif" onclick="DeleteonClick">删除</a>
            <a class="mini-button" img="/miniui_net/scripts/miniui/themes/icons/print.gif" onclick="onPrintClick">打印</a>
        </div>
        <div class="query">
            按名称查询：<input id="name" class="mini-textbox" emptyText="按名称查询" />
            按电话查询：<input id="phone" class="mini-textbox" emptyText="按电话号码查询" /><br /><br />
            按性别查询：<input id="sex" class="mini-textbox" emptyText="按性别查询" />
            按身份证查询：<input id="Idto" class="mini-textbox" emptyText="按身份证查询" />
            <a class="mini-button mini-button-success" onclick="queryClick">查询</a>
        </div>
    </div>
    <div title="center" region="center">
        <div id="datagrid1" class="mini-datagrid" style="width: 100%; height: 100%;"
             url="/Home/UserDataList" idField="USERID">
            <div property="columns">
                <div type="indexcolumn">序列</div>
                <div type="checkcolumn">选择</div>
                <div field="USERNAME" align="center" headerAlign="center" allowSort="true">姓名</div>
                <div field="USERAGE" align="center" headerAlign="center" allowSort="true">年龄</div>
                <div field="USERSEX" align="center" headerAlign="center" allowSort="true" renderer="onGenderRenderer">性别</div>
                <div field="USERCARD" align="center" headerAlign="center" allowSort="true">身份证</div>
                <div field="USERPHONE" align="center" headerAlign="center" allowSort="true">电话号码</div>
                <div field="USERBRITH" align="center" headerAlign="center" allowSort="true" renderer="onBirthdayRenderer">出生日期</div>
            </div>
        </div>
    </div>
</div>
<script src="~/miniui_net/scripts/boot.js"></script>
<script>
    mini.parse();
    var grid = mini.get("datagrid1");
    grid.load();
    // 过滤日期数据
    function onBirthdayRenderer(e) { // 格式化表格日期
        var value = e.value;
        if (value) return mini.formatDate(value, 'yyyy-MM-dd');
        return "";
    }
    // 过滤性别数据
    function onGenderRenderer(e) { // 格式化表格性别数据
        var value = e.value;
        if (value == 1) return "男";
        if (value == 0) return "女";
        return "";
    }
    // 查询数据
    function queryClick() // 条件查询
    {
        var name = mini.get("name");
        var phone = mini.get("phone");
        var sex = mini.get("sex");
        var Idto = mini.get("Idto");
        grid.load({ USERNAME: name.value, USERPHONE: phone.value, USERSEX: sex.value, USERCARD: Idto.value });
    }
    // 新增数据
    function CreateUseronClick()
    {
        mini.open({
            targetWindow: window,
            url: "/Home/CreateUserList",
            title: "新增用户信息", width: 800, height: 500,
            onload: function () {
                var iframe = this.getIFrameEl();
                var data = { action: "新增用户信息", ID: 0 };
                iframe.contentWindow.SetData(data);
            },
            ondestroy: function (action) {
                grid.reload();
            }
        });
    }
    // 更新数据
    function UpdateUseronClick() 
    {
        var row = grid.getSelected();
        if (row) {
        mini.open({
            targetWindow: window,
            url: "/Home/CreateUserList",
            title: "修改用户信息", width: 800, height: 500,
            onload: function () {
                var iframe = this.getIFrameEl();
                var data = { action: "修改用户信息", ID: row.USERID };
                iframe.contentWindow.SetData(data);
            },
            ondestroy: function (action) {
                grid.reload();
            }
        });
        } else {
            alert("请选中一条记录");
        }
    }
    // 删除数据
    function DeleteonClick() { 
        var row = grid.getSelected();
        if (row) {
            if (confirm("确定删除选中记录？")) {
                $.ajax({
                    url: "/Home/DeleteUserList",
                    type: "post",
                    data: { USERID: row.USERID },
                    cache: false,
                    success: function (text) {
                        alert(text.msg);
                        grid.reload();
                    },
                    error: function (text) {
                        alert(text.msg);
                    }
                });
            }
        }
     else {
        alert("请选中一条记录");
       }
    }
    // 打印数据
    function onPrintClick() 
    {
        var row = grid.getSelected();
        if (row) {
            $.ajax({
                url: "/Home/Print_Excel",
                type: "post",
                data: { USERID: row.USERID },
                cache: false,
                success: function (text) {
                    if (text.Statu == 1) {
                        download(text.Data, text.BackUrl);
                        alert(text.Msg);
                    }
                    else {
                        alert(text.Msg);
                    }
                },
                error: function (text) {
                    alert(text.msg);
                }
            });
        }
        else
        {
            alert("请选择要打印的数据");
        }
    }
    function download(fileName, contentOrPath) {
        //# document创建a便签
        let aLink = document.createElement('a');
        /// #download属性值
        aLink.download = fileName;
        // # 追加到document中
        document.body.appendChild(aLink);
        // # data-url为自定性的属性，值是下载的请求路径
        let down_url = contentOrPath;
        // # a标签的href赋值
        aLink.href = down_url;
        //# 触发a标签的click事件，实现文件下载
        aLink.click();
        //# 移除追加的a标签
        document.body.removeChild(aLink);
    }
</script>